package com.dmy.component;

import com.dmy.config.GlobleConfig;

import javax.swing.*;
import java.awt.*;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CompletableFuture;
import java.util.function.Supplier;

public abstract class Food {

    private String name; // 食物名称
    private boolean isLive = true; // 食物是否存活
    private Color color; // 食物颜色
    private int y; // 坐标y轴
    private int x; // 坐标x轴
    private long timeout; // 超时时间

    private JComponent jComponent;
    private Food This;

    public Food() {
        This = this;
       _init();
       _initRandomWZ();
    }

    private void _initRandomWZ() {
        CompletableFuture.supplyAsync(()->this)
                .thenAcceptAsync(T->{
                    while (true){
                        if(System.currentTimeMillis()>=T.timeout){
                            T.randomWZ();
                            resetTimeOut();
                        }
                        checkHit(((MJPanel)jComponent).getSnakes());

                        jComponent.repaint();
                        try {
                            Thread.sleep(20);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    private boolean checkHit(List<Snake> ss) {
        for (Snake s : ss) {
            if(s.getSnakeHead().getCoorNT().getX()+(GlobleConfig.S_SIZE/2)>=This.getX()&&
                    s.getSnakeHead().getCoorNT().getX()+(GlobleConfig.S_SIZE/2)<=This.getX()+GlobleConfig.S_SIZE&&
                    s.getSnakeHead().getCoorNT().getY()+(GlobleConfig.S_SIZE/2)>=This.getY()&&
                    s.getSnakeHead().getCoorNT().getY()+(GlobleConfig.S_SIZE/2)<=This.getY()+GlobleConfig.S_SIZE){
                Color c = this.color;
                _init();
                return  s.addHead(c);
            }
        }
        return false;
    }

    private void resetTimeOut(){
        this.timeout = System.currentTimeMillis()+10000;
    }
    private void _init() {
        this.color = getRandomColor();
        resetTimeOut();
        this.randomWZ();
    }

    private final void randomWZ() {
        this.x= new Random().nextInt(GlobleConfig.WINDOW_WIDTH-GlobleConfig.S_SIZE*2);
        this.y= new Random().nextInt(GlobleConfig.WINDOW_HEIGHT-GlobleConfig.S_SIZE*2);
    }

    public Food(JComponent jComponent) {
        this();
        this.jComponent = jComponent;
    }

    public abstract void paint(Graphics2D g);

    private final Color getRandomColor(){
        return new Color(new Random().nextInt(255)+1,new Random().nextInt(255)+1,new Random().nextInt(255)+1 );
    }

    public Color getColor() {
        return color;
    }

    public void setColor(Color color) {
        this.color = color;
    }

    public long getTimeout() {
        return timeout;
    }

    public void setTimeout(long timeout) {
        this.timeout = timeout;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isLive() {
        return isLive;
    }

    public void setLive(boolean live) {
        isLive = live;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }
}
